package com.elm.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.elm.dao.FoodDao;
import com.elm.model.Food;
import com.elm.model.PageBean;
import com.elm.util.JDBCUtil;
/**

 * 

 * @author pgm

 * 

 */
public class FoodDaoImpl implements FoodDao{

	@Override
	public Food getFoodbyFoodid(int foodid) {
		Food food = new Food();
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select * from food where foodid = ?");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, foodid);
			ResultSet rs = pst.executeQuery();
			if(rs.next()) {
				food.setFoodid(foodid);
				food.setFoodimg(rs.getString("foodimg"));
				food.setFoodname(rs.getString("foodname"));
				food.setFoodprice(rs.getString("foodprice"));
				food.setFoodgenre(rs.getString("foodgenre"));
				food.setSellerid(rs.getInt("sellerid"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block


			e.printStackTrace();
		}
		return food;
	}

	@Override
	public List<Food> findAllfoodBySid(int sellerid,PageBean p) {
		// TODO Auto-generated method stub
		List<Food> foods =new ArrayList<Food>();
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select * from food where food.sellerid =? limit ?,?");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, sellerid);
			pst.setInt(2, p.getStart());
			pst.setInt(3, p.getPageSize());
			ResultSet rs = pst.executeQuery();
			while(rs.next()) {
				Food food = new Food();
				food.setFoodid(rs.getInt("foodid"));
				food.setFoodimg(rs.getString("foodimg"));
				food.setFoodname(rs.getString("foodname"));
				food.setFoodprice(rs.getString("foodprice"));
				food.setFoodgenre(rs.getString("foodgenre"));
				food.setSellerid(rs.getInt("sellerid"));
				foods.add(food);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block


			e.printStackTrace();
		}
		return foods;
	}

	@Override
	public int getFoodCount(int sellerid) {
		int count=0;
		Connection   con  =  JDBCUtil.getConnection();
		StringBuffer  sql = new StringBuffer("select * from food where food.sellerid=?");
		
			PreparedStatement pst;
			try {
				pst = con.prepareStatement(sql.toString());
				pst.setInt(1, sellerid);
				ResultSet rs = pst.executeQuery();
				while(rs.next()){
					count++;
				}
				 JDBCUtil.closeJDBC(pst, con);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		
		return count;
	}
	public List<Food> getFoodbyFoodids(List<Integer> foodids) {
		List<Food> foods = new ArrayList<Food>();
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select * from food where foodid = ?");
		for (int i = 0; i < foodids.size(); i++) {
			try {
				PreparedStatement pst = con.prepareStatement(sql.toString());
				pst.setInt(1, foodids.get(i));
				ResultSet rs = pst.executeQuery();
				if(rs.next()){
					Food f = new Food();
					f.setFoodid(rs.getInt("foodid"));
					f.setFoodimg(rs.getString("foodimg"));
					f.setFoodname(rs.getString("foodname"));
					f.setFoodprice(rs.getString("foodprice"));
					f.setFoodgenre(rs.getString("foodgenre"));
					f.setSellerid(rs.getInt("sellerid"));
					foods.add(f);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return foods;
	}
	public List<Food> getFoodBySellerid(int sellerid, int start, int pageSize) {
		// TODO Auto-generated method stub
	List<Food> foods=new ArrayList<Food>();
	Connection con=JDBCUtil.getConnection();
	StringBuffer sql=new StringBuffer("select foodid,foodname,foodprice,foodgenre,foodimg from  food where sellerid=?");
		sql.append(" limit ?,?");
		try {
			PreparedStatement pst=con.prepareStatement(sql.toString());
		pst.setInt(1, sellerid);
		pst.setInt(2, start);
		pst.setInt(3, pageSize);
		ResultSet rs=pst.executeQuery();
		while (rs.next()) {
			Food f=new Food();
			f.setFoodid(rs.getInt("foodid"));
			f.setFoodname(rs.getString("foodname"));
			f.setFoodprice(rs.getString("foodprice"));
			f.setFoodgenre(rs.getString("foodgenre"));
			f.setFoodimg(rs.getString("foodimg"));
			foods.add(f);
			//System.out.println(f);
		}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			return foods;
	}
//增加菜品
	@Override
	public int insertFood(Food f,int sellerid) {
		// TODO Auto-generated method stub
		int flag=0;
		Connection con=JDBCUtil.getConnection();
		StringBuffer sql=new StringBuffer("insert into food (sellerid,foodname,foodprice,foodimg,foodgenre) values(?,?,?,?,?)");
		try {
			PreparedStatement pst=con.prepareStatement(sql.toString());
			pst.setInt(1, sellerid);
			pst.setString(2, f.getFoodname());
			pst.setString(3, f.getFoodprice());
			pst.setString(4, f.getFoodimg());
			pst.setString(5, f.getFoodgenre());
			
			int i=pst.executeUpdate();
	if (i>0) {
		flag=1;
	}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
//修改菜品
	@Override
	public int updateFood(Food f) {
		// TODO Auto-generated method stub
		int flag=0;
		Connection con=JDBCUtil.getConnection();
		StringBuffer sql=new StringBuffer("update food set foodname=?,foodprice=?,foodgenre=?,foodimg=?  where foodid=?");
		try {
			PreparedStatement pst=con.prepareStatement(sql.toString());
			pst.setString(1, f.getFoodname());
			pst.setString(2, f.getFoodprice());
			pst.setString(3, f.getFoodgenre());
			pst.setString(4, f.getFoodimg());
			pst.setInt(5, f.getFoodid());
			int i=pst.executeUpdate();
			if (i>0) {
				flag=1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}
//模糊查询
	@Override
	public int getFoodCount(int sellerid, String searchname) {
		// TODO Auto-generated method stub
		int fcount=0;
		Connection con=JDBCUtil.getConnection();
		StringBuffer sql=new StringBuffer("select * from  food where  sellerid=? and foodname=? ");
		try {
			PreparedStatement pst=con.prepareStatement(sql.toString());
		pst.setInt(1, sellerid);
		pst.setString(2, "%"+searchname+"%");
		ResultSet rs=pst.executeQuery();
		while (rs.next()) {
			fcount++;
		}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fcount;
	}

	@Override
	public List<Food> getFoodBySellerid(int sellerid, int start, int pageSize, String searchname) {
		// TODO Auto-generated method stub
		List<Food> foods=new ArrayList<Food>(); 
		Connection con=JDBCUtil.getConnection();
		StringBuffer sql=new StringBuffer("select foodid ,foodname,foodgenre ,foodprice,foodimg FROM food where  sellerid=? ");
		sql.append(" and foodname like ? ");
		sql.append(" limit ?,?");
		try {
			PreparedStatement   pst =  con.prepareStatement(sql.toString());
			pst.setInt(1, sellerid);
			pst.setString(2, "%"+searchname+"%");
			pst.setInt(3, start);
			pst.setInt(4, pageSize);
			ResultSet  rs = pst.executeQuery();
			while (rs.next()) {
				Food food=new Food();
				food.setFoodid(rs.getInt("foodid"));
				food.setFoodname(rs.getString("foodname"));
				food.setFoodgenre(rs.getString("foodgenre"));
				food.setFoodprice(rs.getString("foodprice"));
				foods.add(food);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return foods;
	}
//删除菜品
	@Override
	public boolean deleteFood(String arr) {
		// TODO Auto-generated method stub
		boolean flag=false;
			
			String[] sids= arr.split(",");
			int[] ids = new int[sids.length];
			for (int i = 0; i < ids.length; i++) {
				ids[i]=Integer.parseInt(sids[i]);
			}
			Connection con=JDBCUtil.getConnection();
			String sql1="";
			for (int i = 0; i < ids.length; i++) {
				sql1+="'"+ids[i]+"'";
				if(i<ids.length-1){
					sql1+=",";
				}
			}
			StringBuffer sql = new StringBuffer("delete from food where foodid in ("+sql1+")");
			try {
				
				PreparedStatement pst=con.prepareStatement(sql.toString());
				
				int i=pst.executeUpdate();
				if (i>0) {
					flag=true;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
		
		return flag;
	}

}
